package com.lightChat.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @author sxt
 * @create 2019-11-13 22:09
 */
public class WsChannelInitializer extends ChannelInitializer<SocketChannel> {


    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast(new HttpServerCodec());

        pipeline.addLast(new ChunkedWriteHandler());//处理大数据流

        pipeline.addLast(new HttpObjectAggregator(1024*64));//几乎所有的netty编程都会用到此handler



        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));//支持webSocket协议的处理器

        pipeline.addLast(new ChatHandler());

        //心跳的支持
        pipeline.addLast(new IdleStateHandler(8, 8, 12));

        pipeline.addLast(new HeartBeatHandler());

    }
}
